package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.utils.common.AdapterIntegerDefault0;
import com.lws.basic.utils.common.DoubleDefault0Adapter;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.ResidentDetailBean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//居民详情
public class ResidentDetailActivity extends BaseActivity {


    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_sex)
    TextView mTvSex;
    @BindView(R.id.tv_birth)
    TextView mTvBirth;
    @BindView(R.id.tv_id)
    TextView mTvId;
    @BindView(R.id.tv_work)
    TextView mTvWork;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;
    @BindView(R.id.tv_whcd)
    TextView mTvWhcd;
    @BindView(R.id.tv_nation)
    TextView mTvNation;
    @BindView(R.id.tv_hyzk)
    TextView mTvHyzk;
    @BindView(R.id.tv_sfqy)
    TextView mTvSfqy;
    @BindView(R.id.tv_zy)
    TextView mTvZy;
    @BindView(R.id.tv_zffs)
    TextView mTvZffs;
    private ResidentDetailBean.DataBean mDataBean;


    @Override
    public int setLayoutId() {
        return R.layout.activity_resident_detail;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        getDate();

    }


    private void getDate(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<ResidentDetailBean>() {
            @Override
            public void subscribe(ObservableEmitter<ResidentDetailBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("jmid", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("jmId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkgoNetwork.post(Constant.resident_detail, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json,ResidentDetailBean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<ResidentDetailBean>() {
            @Override
            public void onNext(ResidentDetailBean bean) {
                dismissLoading();
                if (bean.getData() != null) {
                    mDataBean = bean.getData();
                    mTvName.setText(mDataBean.getXm());
                    if (mDataBean.getXb() == 1) {
                        mTvSex.setText("男");
                    } else {
                        mTvSex.setText("女");
                    }
                    mTvId.setText(mDataBean.getSfzh());
                    mTvPhone.setText(mDataBean.getBrdh());
                    if (!TextUtils.isEmpty(mDataBean.getCsrq())){
                       mTvBirth.setText(mDataBean.getCsrq());
                    }
                   if (mDataBean.getMz()==0){
                       mTvNation.setText("未知");

                   }else {
                       mTvNation.setText(User.getNation(mDataBean.getMz()));
                   }
                    if (mDataBean.getSfqy() == 1) {
                        mTvSfqy.setText("已签约");
                    } else {
                        mTvSfqy.setText("未签约");
                    }
                    if (TextUtils.isEmpty(mDataBean.getGzdw())){
                        mTvWork.setText("无");

                    }else {
                        mTvWork.setText(mDataBean.getGzdw()+"");
                    }

                    switch (mDataBean.getHyzk()) {
                        case 1:
                            mTvHyzk.setText("未婚");
                            break;
                        case 2:
                            mTvHyzk.setText("已婚");
                            break;
                        case 3:
                            mTvHyzk.setText("丧偶");
                            break;
                        case 4:
                            mTvHyzk.setText("离婚");
                            break;
                        case 5:
                            mTvHyzk.setText("未说明婚姻状况");
                            break;

                    }
                    if (mDataBean.getWhcd()==0){
                        mTvWhcd.setText("不详");

                    }else {
                        mTvWhcd.setText(User.getEducation(mDataBean.getWhcd()));
                    }
                    mTvZy.setText("职业：  \n          "+zy(mDataBean.getZy()));//职业
                    mTvZffs.setText("支付方式:  \n          "+zffs(User.numList(mDataBean.getYlfyzffs()), mDataBean.getYlfyzffsQt()));

                } else {
                    MyToast.show(bean.getMsg());
                }
            }
        });
    }


    @OnClick(R.id.tv_detail)
    public void onViewClicked() {
        Intent intent  = new Intent(this,ResidentHealthDetailActivity.class);
        intent.putExtra("data", mDataBean);
        startActivity(intent);
    }

    //支付方式
    private  String zffs(List<String> list, String qt){
        String s ="";
        for (int i=0; i< list.size(); i++) {
            switch (list.get(i)) {
                case "0":
                    s += "城镇职工基本医疗保险  ";
                    break;
                case "1":
                    s += "城镇居民基本医疗保险  ";
                    break;
                case "2":
                    s += "新型农村合作医疗  ";
                    break;
                case "3":
                    s += "贫困救助  ";
                    break;
                case "4":
                    s += "商业医疗保险  ";
                    break;
                case "5":
                    s += "全公费  ";
                    break;
                case "6":
                    s += "全自费  ";
                    break;
                case "7":
                    s += qt;

            }
        }
        return s;

    }

    //职业
    private  String zy(int index){
        String s ="";
        switch (index){
            case 0:
                s = "国家机关、党群组织、企业、事业单位负责人";
                break;
            case 1:
                s = "专业技术人员";
                break;
            case 2:
                s = "办事人员和有关人员";
                break;
            case 3:
                s = "商业、服务业人员";
                break;
            case 4:
                s = "农、林、牧、渔、水利业生产人员";
                break;
            case 5:
                s = "生产、运输设备操作人员及有关人员";
                break;
            case 6:
                s = "军人";
                break;
            case 7:
                s = "不便分类的其他从业人员";
                break;
            case 8:
                s = "无职业";
                break;
        }
        return s;

    }
}
